import Clipboard from 'clipboard'
import { Toast, Dialog } from 'vant'
import http from '@/http'
import { api } from '@/http/api'
import { WAY_BILL_MAP_KEY } from '@/utils/constant'
import storage from 'good-storage'

const useOpearation = (state: any, router: any, onSearch: any): any => {
  // 点击复制运单号
  const handleCopy = () => {
    const clipboard = new Clipboard('.copyNo')
    clipboard.on('success', e => {
      Toast('复制成功')
      //  释放内存
      clipboard.destroy()
    })
    clipboard.on('error', e => {
      // 不支持复制
      Toast('当前手机不支持自动复制')
      // 释放内存
      clipboard.destroy()
    })
  }

  //点击进入列表详情也
  const handleDetail = (item: any) => {
    router.push({
      name: 'WaybillDetail',
      query: {
        id: item.id,
        waybillStatus: item.billOrderStatus,
        dataType: item.dataType
      }
    })
  }

  // 查看单据
  const onCheckBill = (
    item: any,
    billStatusType: number,
    type: '1' | '2' | string,
    showTab = 0,
    showOpeartion = 1
  ): void => {
    router.push({
      name: 'Receipt',
      query: {
        billId: item.id,
        billStatusType,
        showTab,
        type,
        showOpeartion,
        payStatus: item.payStatus
      }
    })
  }

  // 取消订单
  const handleCancelOrder = (id: string, callback?: any) => {
    Dialog.confirm({
      title: '',
      message: '您确认取消当前订单吗'
    })
      .then(() => {
        sureRequest()
      })
      .catch(() => {
        //
      })
    function sureRequest() {
      http<string>({
        url: api.updateCancelOrder,
        method: 'post',
        data: { id }
      }).then((res: any) => {
        // onSearch()
        if (callback && typeof callback === 'function') callback()
      })
    }
  }

  // 点击签收
  const handleConfirm = (id = '', payType: string) => {
    state.temporaryNo = id
    if (payType !== '2') {
      confirmReceipt()
      return
    }

    // 显示密码输入框
    state.passwordPoppup = true
    state.payPassword = ''
  }

  // 付款
  const paymentOrder = (value: string) => {
    const password: string = state.payPassword + value
    if (password.length !== 6) return
    confirmReceipt(password)
  }

  // 确认签收
  const confirmReceipt = (password = '') => {
    http<string>({
      url: api.confirmSignFor,
      method: 'post',
      data: { billId: state.temporaryNo, password }
    })
      .then((res: any) => {
        state.passwordPoppup = false
        Toast.success('签收成功')
        state.activeData.active === 0 ? onSearch() : state.activeData.active++
      })
      .catch(() => {
        state.passwordPoppup = false
      })
  }

  //联系货主
  const goTelphone = (val: any) => {
    if (!val) return
    state.driverMobile = val.driverMobile ? val.driverMobile : val.carrierMobile
    state.isTelPhoneShow = true
  }

  // 指派司机
  const handleAssign = (id: string) => {
    router.push({
      name: 'AssignOrder',
      params: { id }
    })
  }

  //去评价
  const onGoEvaluate = (item: any) => {
    router.push({ name: 'Evaluate', params: { params: JSON.stringify(item) } })
  }

  // 查看地图
  const handleEnterMap = (detail: any): void => {
    storage.session.set(WAY_BILL_MAP_KEY, detail)
    router.push({
      path: '/waybill/transport-map'
      // query: { orderStaus: detail.orderStaus,
      //    detail.sendLatitude,
      //     detail.sendLongitude,
      //      detail.receiveLatitude,
      //       detail.receiveLongitude
      //      }
    })
  }

  return {
    handleAssign,
    goTelphone,
    confirmReceipt,
    paymentOrder,
    handleConfirm,
    handleCancelOrder,
    onCheckBill,
    handleDetail,
    handleCopy,
    onGoEvaluate,
    handleEnterMap
  }
}

export default useOpearation
